<template>
  <div class="supplier-project-list">
    <!-- 搜索表单 -->
    <el-card class="search-card">
      <el-form 
        :model="searchForm" 
        :inline="true" 
        label-width="100px"
        @submit.prevent="handleSearch"
      >
        <el-form-item label="项目名称">
          <el-input 
            v-model="searchForm.projectName" 
            placeholder="请输入项目名称"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        
        <el-form-item label="项目编号">
          <el-input 
            v-model="searchForm.projectCode" 
            placeholder="请输入项目编号"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        
        <el-form-item label="供应商名称">
          <el-input 
            v-model="searchForm.supplierName" 
            placeholder="请输入供应商名称"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        
        <el-form-item label="供应商类型">
          <el-select 
            v-model="searchForm.supplierType" 
            placeholder="请选择供应商类型"
            clearable
            style="width: 200px"
          >
            <el-option label="工程类" value="工程类" />
            <el-option label="技服类" value="技服类" />
            <el-option label="物资类" value="物资类" />
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
          <el-button type="success" @click="handleExport">导出</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <template #header>
        <div class="card-header">
          <span>供应商项目列表</span>
        </div>
      </template>
      
      <el-table 
        :data="tableData" 
        v-loading="loading"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="序号" width="80" align="center" />
        
        <el-table-column prop="projectName" label="项目名称" min-width="150" />
        
        <el-table-column prop="supplierName" label="供应商名称" min-width="150">
          <template #default="{ row }">
            <span v-if="row.supplierName">{{ row.supplierName }}</span>
            <span v-else style="color: #999">未关联</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="contractDate" label="合同签订日期" width="120" align="center">
          <template #default="{ row }">
            <span v-if="row.contractDate">{{ formatDate(row.contractDate) }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="supplierType" label="供应商类别" width="100" align="center">
          <template #default="{ row }">
            <span v-if="row.supplierType">{{ row.supplierType }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="supplierCode" label="供应商编码" width="120" align="center">
          <template #default="{ row }">
            <span v-if="row.supplierCode">{{ row.supplierCode }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="amount" label="金额（万元）" width="120" align="center">
          <template #default="{ row }">
            <span v-if="row.amount">{{ row.amount }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="currency" label="币种" width="80" align="center">
          <template #default="{ row }">
            <span v-if="row.currency">{{ row.currency }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="{ row }">
            <el-button 
              type="primary" 
              size="small" 
              @click="handleSelectSupplier(row)"
            >
              选择供应商
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <div class="pagination-wrapper">
        <el-pagination
          v-model:current-page="pagination.pageNum"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { projectService } from '../services/supplierService.js';

export default {
  name: 'SupplierProjectList',
  setup() {
    const router = useRouter();
    
    // 响应式数据
    const loading = ref(false);
    const tableData = ref([]);
    
    // 搜索表单
    const searchForm = reactive({
      projectName: '',
      projectCode: '',
      supplierName: '',
      supplierType: ''
    });
    
    // 分页数据
    const pagination = reactive({
      pageNum: 1,
      pageSize: 20,
      total: 0
    });

    // 获取项目列表数据
    const getProjectList = async () => {
      try {
        loading.value = true;
        const params = {
          ...searchForm,
          pageNum: pagination.pageNum,
          pageSize: pagination.pageSize
        };
        
        const response = await projectService.getProjectList(params);
        
        if (response.code === 200) {
          tableData.value = response.data.records || [];
          pagination.total = response.data.total || 0;
        } else {
          ElMessage.error(response.message || '获取数据失败');
        }
      } catch (error) {
        console.error('获取项目列表失败:', error);
        ElMessage.error('获取数据失败，请稍后重试');
      } finally {
        loading.value = false;
      }
    };

    // 搜索处理
    const handleSearch = () => {
      pagination.pageNum = 1;
      getProjectList();
    };

    // 重置处理
    const handleReset = () => {
      Object.keys(searchForm).forEach(key => {
        searchForm[key] = '';
      });
      pagination.pageNum = 1;
      getProjectList();
    };

    // 导出处理
    const handleExport = () => {
      ElMessage.info('导出功能待实现');
    };

    // 选择供应商处理
    const handleSelectSupplier = (row) => {
      // 跳转到项目供应商详情页面
      router.push({
        name: 'ProjectSupplierDetail',
        params: { id: row.id }
      });
    };

    // 分页大小改变
    const handleSizeChange = (newSize) => {
      pagination.pageSize = newSize;
      pagination.pageNum = 1;
      getProjectList();
    };

    // 当前页改变
    const handleCurrentChange = (newPage) => {
      pagination.pageNum = newPage;
      getProjectList();
    };

    // 格式化日期
    const formatDate = (date) => {
      if (!date) return '';
      const d = new Date(date);
      return d.toLocaleDateString('zh-CN');
    };

    // 组件挂载时获取数据
    onMounted(() => {
      getProjectList();
    });

    return {
      loading,
      tableData,
      searchForm,
      pagination,
      getProjectList,
      handleSearch,
      handleReset,
      handleExport,
      handleSelectSupplier,
      handleSizeChange,
      handleCurrentChange,
      formatDate
    };
  }
};
</script>

<style scoped>
.supplier-project-list {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
}

.pagination-wrapper {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-form--inline .el-form-item {
  margin-right: 20px;
  margin-bottom: 15px;
}
</style> 